import { reqCountDown, reqBannerList, reqGrouponList ,reqGoodsList} from "../../http/api";
//初始值
const initState = {
  countDown: {},
  banner: [],
  grouponList: [],
  n: 0,
  goods: [],
};

//action types
const types = {
  HOME_CHANGE_COUNT: "HOME_CHANGE_COUNT",
  HOME_CHANGE_BANNER: "HOME_CHANGE_BANNER",
  HOME_CHANGE_GROUPON: "HOME_CHANGE_GROUPON",
  HOME_CHANGE_N: "HOME_CHANGE_N",
  HOME_CHANGE_GOODS: "HOME_CHANGE_GOODS",
};

//action creator
export const actions = {
  changeCount: (countDown) => ({
    type: types.HOME_CHANGE_COUNT,
       countDown,  //修改数据 倒计时
   
  }),
  reqCount: () => {
    return (dispatch, getState) => {
      reqCountDown().then((res) => {
        dispatch(actions.changeCount(res.data.list ? res.data.list : {}));
      });
    };
  },
  //修改数据 banner 新人
  changeBanner: (banner) => ({
    type: types.HOME_CHANGE_BANNER,
    banner,
  }),
  reqBanner: () => {
    return (dispatch, getStates) => {
      reqBannerList().then((res) => {
        if (res.data.code === 200) {
          dispatch(actions.changeBanner(res.data.list ? res.data.list : []));
        }
      });
    };
  },
  //修改数据 banner 万人团
  changeGroupon: (grouponList) => ({
    type: types.HOME_CHANGE_GROUPON,
    grouponList,
  }),
  reqgounpon: () => {
    return (dispatch, getState) => {
      reqGrouponList().then((res) => {
        if (res.data.code === 200) {
          dispatch(actions.changeGroupon(res.data.list ? res.data.list : []));
        }
      });
    };
  },
  // 修改n
  changeN: (n) => ({
    type: types.HOME_CHANGE_N,
    n,
  }),
  //获得goods 修改goods
  changeGoods: (goods) => ({
    type: types.HOME_CHANGE_GOODS,
    goods,
  }),
  reqgoods: () => {
    return (dispatch, getState) => {
      reqGoodsList().then((res) => {
        if (res.data.code === 200) {
          dispatch(actions.changeGoods(res.data.list ? res.data.list : []));
        }
      });
    };
  },
};

//reducer
export const homeReducer = (state = initState, action) => {
  switch (action.type) {
    case types.HOME_CHANGE_COUNT:
      return {
        ...state,
        countDown: action.countDown,
      };
    case types.HOME_CHANGE_BANNER:
      return {
        ...state,
        banner: action.banner,
      };
    case types.HOME_CHANGE_GROUPON:
      return {
        ...state,
        grouponList: action.grouponList,
      };
    case types.HOME_CHANGE_N:
      return {
        ...state,
        n: action.n,
      };
      case types.HOME_CHANGE_GOODS:
      return {
        ...state,
        goods: action.goods,
      };
    default:
      return state;
  }
};

//导出数据

export const getCount = (state) => state.home.countDown;
export const getBanner = (state) => state.home.banner;
export const getgroupon = (state) => state.home.grouponList;
export const getN = (state) => state.home.n;
export const getGoods = (state) => state.home.goods;
